﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;
using System.Data.SqlClient;

namespace QL_Lichvantai
{
    public partial class frm_Nhanvien : Form
    {
        public static DataTable dt;
        public frm_Nhanvien()
        {
            InitializeComponent();
        }

        Nhanvien_DTO nv;

        public void GetData(Nhanvien_DTO nv)
        {
            this.nv = nv;
        }

        public string Vaitro(string vt)
        {
            return (vt == "1") ? "Tài xế" : "Phụ lái";
        }
        private void frm_Nhanvien_Load(object sender, EventArgs e)
        {
            try
            {

                // load thong tin nhan vien

                lb_hoten.Text = nv.Hoten;
                lb_chucvu.Text = Vaitro(nv.Vaitro);
                pictureBox1.Image = Image.FromFile("..\\..\\Images\\nhanvien.jpg");
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;



                cmb_tuyenduong.Enabled = false;
                rad_tatca.Checked = true;



                //load tuyen duong

                SqlConnection cn = DataProvider.ConnectDB();

                DataTable dt = new DataTable();
                SqlCommand cmd = new SqlCommand("LoadTuyenDuong", cn);

                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);

                //DataRow irow = dt.NewRow();
                //irow["MaTuyenDuong"] = 0;
                //irow["TenTuyenDuong"] = "";

                //dt.Rows.InsertAt(irow, 0);

                cmb_tuyenduong.DataSource = dt;
                cmb_tuyenduong.ValueMember = "MaTuyenDuong";
                cmb_tuyenduong.DisplayMember = "TenTuyenDuong";


               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           

        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tab_xemlich.SelectedTab == tab_Lichlamviec)
            {
                Load_LichLamViec();
                return;
           
            }
            if (tab_xemlich.SelectedTab == tab_xemlichvanchuyen)
            {
                xem();
                return;
            }
        }


        private void rad_tuyenduong_CheckedChanged(object sender, EventArgs e)
        {
            if (rad_tuyenduong.Checked == true)
            {
                cmb_tuyenduong.Enabled = true;
                date_ngaydi.Enabled = false;
                date_ngayden.Enabled = false;
                return;
            }
            if (rad_tatca.Checked == true)
            {
                cmb_tuyenduong.Enabled = false;
                date_ngaydi.Enabled = false;
                date_ngayden.Enabled = false;
                return;

            }
            if (rad_thoigian.Checked == true)
            {
                cmb_tuyenduong.Enabled = false;
                date_ngayden.Enabled = true;
                date_ngaydi.Enabled = true;
            }
        }

        private void btn_Xem_Click(object sender, EventArgs e)
        {

           
           
        }

        private void btn_Xem_Click_1(object sender, EventArgs e)
        {
            xem();
        }


        public void Load_LichLamViec()
        {
            Nhanvien_BUS bus = new Nhanvien_BUS();
            DataSet ds = bus.LichLamViec(nv.Ma_nv);
            gv_Lichlamviec.DataSource = ds.Tables[0];
        }
        private void bt_HuyLich_Click(object sender, EventArgs e)
        {
            try
            {
                int iRow = gv_Lichlamviec.CurrentRow.Index;
                int mavc = int.Parse(gv_Lichlamviec.Rows[iRow].Cells[0].Value.ToString());
                int maxe = int.Parse(gv_Lichlamviec.Rows[iRow].Cells[1].Value.ToString());
                
                DialogResult dialogResult = MessageBox.Show("Bạn muốn hủy ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    Nhanvien_BUS bus = new Nhanvien_BUS();
                    int kq = bus.Huy_LichLamViec(nv,mavc,maxe);
                    
                    Load_LichLamViec();
                    MessageBox.Show(nv.Msg);
                  
                }
                else if (dialogResult == DialogResult.No)
                {
                    return;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

      

        private void rad_tatca_CheckedChanged(object sender, EventArgs e)
        {
            if (rad_tuyenduong.Checked == true)
            {
                cmb_tuyenduong.Enabled = true;
                date_ngaydi.Enabled = false;
                date_ngayden.Enabled = false;
                return;
            }
            if (rad_tatca.Checked == true)
            {
                cmb_tuyenduong.Enabled = false;
                date_ngaydi.Enabled = false;
                date_ngayden.Enabled = false;
                return;

            }
            if (rad_thoigian.Checked == true)
            {
                cmb_tuyenduong.Enabled = false;
                date_ngayden.Enabled = true;
                date_ngaydi.Enabled = true;
            }
        }

      

        private void cmb_td_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            try
            {
                cmb_xe.Items.Clear();
                // load combobox xe
                string s = cmb_td.SelectedItem.ToString();
                int mavc_selected = Convert.ToInt32(s);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (Convert.ToInt32(dt.Rows[i][0]) == mavc_selected)
                    {
                        int maxe = Convert.ToInt32(dt.Rows[i][3]);
                        cmb_xe.Items.Add(maxe.ToString());

                    }
                }
            }
            catch (Exception ex )
            {

                MessageBox.Show(ex.Message);
            }
            
        }

        private void btn_dangky_Click(object sender, EventArgs e)
        {
            try
            {
                int mavanchuyen = 0;
                int maxe = 0 ;
                int manhanvien = Convert.ToInt32(nv.Ma_nv);
                int vaitro = Convert.ToInt32(nv.Vaitro);

                ErrorDTO error = new ErrorDTO();

                if (cmb_td.SelectedItem != null)
                {
                    mavanchuyen = Convert.ToInt32(cmb_td.SelectedItem);
                }
                if (cmb_xe.SelectedItem != null)
                {
                    maxe = Convert.ToInt32(cmb_xe.SelectedItem);
                }



                SqlConnection cn = DataProvider.ConnectDB();

                DataTable dt = new DataTable();
                SqlCommand cmd = new SqlCommand("sp_DKLichlamviec", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@mavanchuyen", mavanchuyen));
                cmd.Parameters.Add(new SqlParameter("@maxe", maxe));
                cmd.Parameters.Add(new SqlParameter("@manhanvien", manhanvien));
                cmd.Parameters.Add(new SqlParameter("@vaitro", vaitro));
                SqlParameter output = cmd.Parameters.Add(new SqlParameter("@error", SqlDbType.NVarChar, 100));
                output.Direction = ParameterDirection.Output;

               
                cmd.ExecuteNonQuery(); 

                cn.Close();
                error.Msg = output.Value.ToString();
                if (error.Msg != "")
                {
                    MessageBox.Show(error.Msg);
                    xem();
                }
                else
                {
                    MessageBox.Show("Đăng ký thành công");
                    xem();
                   
                    
                }

                
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);

            }

            
        }



        public void xem()
        {
            try
            {
                if (cmb_td.Items.Count != 0)
                {
                    cmb_td.Items.Clear();

                }

                if (cmb_xe.Items.Count != 0)
                {
                    cmb_xe.Items.Clear();
                }
                lvl_danhsachlich.Items.Clear();

                lbl_soluongxe.Text = " ";

                dt = new DataTable();

                int matuyenduong;

                ErrorDTO error = new ErrorDTO();


                Lichvanchuyen_BUS xemlich = new Lichvanchuyen_BUS();

                if (rad_tatca.Checked == true)
                {
                    dt = xemlich.XemLichVanChuyen_tatca(error);
                }
                else if (rad_tuyenduong.Checked == true)
                {
                    matuyenduong = Convert.ToInt32(cmb_tuyenduong.SelectedValue);
                    dt = xemlich.XemLichVanChuyen_TheoTuyenDuong(matuyenduong, error);
                }
                else  // radio button thoi gian checked
                {
                    DateTime ngaydi = date_ngaydi.Value;
                    DateTime ngayden = date_ngayden.Value;

                    dt = xemlich.XemLichVanChuyen_theothoigian(ngaydi, ngayden, error);
                }



                if (error.Msg == "")
                {

                    lbl_soluongxe.Text = "Tổng cộng có " + error.slxe + " xe";

                    //add data vao listview;


                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ListViewItem lvi = new ListViewItem(dt.Rows[i][0].ToString());
                        lvi.SubItems.Add(dt.Rows[i][1].ToString());   // add diembatdau
                        lvi.SubItems.Add(dt.Rows[i][2].ToString());   // add diemketthuc
                        lvi.SubItems.Add(dt.Rows[i][3].ToString());   // add maxe
                        lvi.SubItems.Add(dt.Rows[i][4].ToString());   // add khoang cach


                        if (dt.Rows[i][5].ToString() != "")
                            lvi.SubItems.Add(Convert.ToDateTime(dt.Rows[i][5]).ToShortDateString());   // add ngaydi
                        else
                            lvi.SubItems.Add(dt.Rows[i][5].ToString());


                        if (dt.Rows[i][6].ToString() != "")
                            lvi.SubItems.Add(Convert.ToDateTime(dt.Rows[i][6]).ToShortDateString());   // add ngayden
                        else
                            lvi.SubItems.Add(dt.Rows[i][6].ToString());


                        lvi.SubItems.Add(dt.Rows[i][7].ToString());   // add taixetrong
                        lvi.SubItems.Add(dt.Rows[i][8].ToString());   // add phulaitrong
                        lvl_danhsachlich.Items.Add(lvi);

                    }


                    // load combobox mavanchuyen
                    DataTable dt_mavanchuyen = new DataTable();
                    dt_mavanchuyen.Columns.Add("MaVanChuyen");

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int kt = 0;
                        int mavc = Convert.ToInt32(dt.Rows[i][0]);
                        for (int j = 0; j < dt_mavanchuyen.Rows.Count; j++)
                        {
                            int mavc1 = Convert.ToInt32(dt_mavanchuyen.Rows[j][0]);
                            if (mavc == mavc1)
                            {
                                kt = 1;
                                break;
                            }

                        }
                        if (kt == 0)
                        {
                            // add mavanchuyen vao dt_mavanchuyen
                            DataRow dr = dt_mavanchuyen.NewRow();
                            dr[0] = mavc;
                            dt_mavanchuyen.Rows.Add(dr);


                        }
                    }
                    for (int i = 0; i < dt_mavanchuyen.Rows.Count; i++)
                    {
                        cmb_td.Items.Add(dt_mavanchuyen.Rows[i][0]).ToString();
                    }

                }


                else
                {
                    lbl_soluongxe.Text = "không có xe nào";
                    MessageBox.Show(error.Msg);
                }
                    
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void rad_thoigian_CheckedChanged(object sender, EventArgs e)
        {
            if (rad_tuyenduong.Checked == true)
            {
                cmb_tuyenduong.Enabled = true;
                date_ngaydi.Enabled = false;
                date_ngayden.Enabled = false;
                return;
            }
            if (rad_tatca.Checked == true)
            {
                cmb_tuyenduong.Enabled = false;
                date_ngaydi.Enabled = false;
                date_ngayden.Enabled = false;
                return;

            }
            if (rad_thoigian.Checked == true)
            {
                cmb_tuyenduong.Enabled = false;
                date_ngayden.Enabled = true;
                date_ngaydi.Enabled = true;
            }
        }

        

       
    }
}
